<?php
/**
 * @deprecated
 * @link https://panxsoft.coding.net/p/Beauty-Mirror-Web/task/158988#comment-144601
 * 客户后台，化妆品材质的管理
 * Author: lyun
 * Date: 2018/9/3
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin\Customer\Product;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Customer\Product\CosmeticMaterialStoreRequest;
use App\Http\Requests\Admin\Customer\Product\CosmeticMaterialUpdateRequest;
use App\Models\Admin\CosmeticMaterial;
use App\Services\Utils\Guard;
use Illuminate\Http\Request;

class CosmeticMaterialController extends ApiController
{
    const ROUTES = [
        'index'   => 'api.admin.customer.cosmetic-material.index',
        'store'   => 'api.admin.customer.cosmetic-material.store',
        'update'  => 'api.admin.customer.cosmetic-material.update',
        'destroy' => 'api.admin.customer.cosmetic-material.delete',
    ];

    /**
     * Display a listing of the resource.
     * @test
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $customer = Guard::customer();

        $cosmetic_materials = CosmeticMaterial::filterCustomer($customer->id)
            ->oldest('sort')
            ->paginate($request->per_page ?? 15);

        return $this->response(compact('cosmetic_materials'));
    }

    /**
     * Store a newly created resource in storage.
     * @test
     *
     * @param  CosmeticMaterialStoreRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CosmeticMaterialStoreRequest $request)
    {
        $cosmetic_material = $this->assembleCosmeticMaterialData($request);

        $stored = CosmeticMaterial::create($cosmetic_material);

        return $this->response(compact('stored'));
    }

    /**
     * Update the specified resource in storage.
     * @test
     *
     * @param  CosmeticMaterialUpdateRequest  $request
     * @param  int  $id
     * @throws \Exception
     * @return \Illuminate\Http\Response
     */
    public function update(CosmeticMaterialUpdateRequest $request, $id)
    {
        $customer = Guard::customer();

        /** @var CosmeticMaterial $cosmetic_material */
        $cosmetic_material = CosmeticMaterial::filterCustomer($customer->id)
            ->find($id);
        $this->checkItemExist($cosmetic_material);

        $cosmetic_material_data = $this->assembleCosmeticMaterialData($request);

        $updated = $cosmetic_material->update($cosmetic_material_data);

        return $this->response(compact('updated'));
    }

    /**
     * Remove the specified resource from storage.
     * @test
     *
     * @param  int  $id
     * @throws \Exception
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $customer = Guard::customer();

        /** @var CosmeticMaterial $cosmetic_material */
        $cosmetic_material = CosmeticMaterial::filterCustomer($customer->id)
            ->find($id);
        $this->checkItemExist($cosmetic_material);
        $this->checkUnless($cosmetic_material->products()->count(), trans('admin.cosmetic_material_has_products'));

        $deleted = $cosmetic_material->delete();

        return $this->response(compact('deleted'));
    }

    /**
     * @param CosmeticMaterialUpdateRequest|CosmeticMaterialStoreRequest|Request $request
     * @return array
     */
    private function assembleCosmeticMaterialData(Request $request)
    {
        return [
            'customer_id' => Guard::customer()->id,
            'name'        => $request->name,
            'sort'        => $request->sort ?? 10000,
        ];
    }
}
